#!/bin/sh
cd ${0%/*} || exit 1    # run from this directory

# Source tutorial run functions
. $WM_PROJECT_DIR/bin/tools/RunFunctions

runApplication blockMesh

# Refine Mesh Near Outlet Section
i=1
while [ "$i" -lt 3 ] ; do # refine each baffle
    echo "Refine outlet section mesh." ${i}
    cp system/topoSetDict.section${i} system/topoSetDict
    cp system/refineMeshDict.section${i} system/refineMeshDict
    runApplication topoSet
    runApplication refineMesh -overwrite
    mv log.topoSet log.topoSet.section${i}
    mv log.refineMesh log.refineMesh.section${i}
    i=$((i + 1))
done

# Create Outlet2
cp system/topoSetDict.outlet2 system/topoSetDict
runApplication topoSet
runApplication createPatch -overwrite
mv log.topoSet log.topoSet.outlet2

# Clean up
rm system/topoSetDict

# Create Baffles
runApplication createBaffles -overwrite

# Refine Boundary Layer Mesh
runApplication refineWallLayer -overwrite "(walls)" 0.3
mv log.refineWallLayer log.refineWallLayer.walls
i=1
while [ "$i" -lt 26 ] ; do # refine each baffle
    echo "Refine baffle mesh." ${i}
    refineWallLayer -overwrite "(baffle${i})" 0.3 >> log.refineWallLayer.baffle${i}
    refineWallLayer -overwrite "(baffle${i})" 0.3 >> log.refineWallLayer.baffle${i}
    i=$((i + 1))
done

# Renumber Mesh
runApplication renumberMesh -overwrite
#------------------------------------------------------------------------------
